﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QCYHApp
{
    public partial class CustomToolTip : UserControl
    {
        public CustomToolTip()
        {
            InitializeComponent();
            this.OwnerDraw = true;
            myTextFormat.FormatFlags = StringFormatFlags.LineLimit;
            myTextFormat.Alignment = StringAlignment.Near;
            myTextFormat.LineAlignment = StringAlignment.Center;
            myTextFormat.Trimming = StringTrimming.None;
            this.Popup += new PopupEventHandler(CustomizedToolTip_Popup);
            this.Draw += new DrawToolTipEventHandler(CustomizedToolTip_Draw);
        }

        void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
        {
            if (OwnerDraw)
            {
                if (!myAutoSize)
                {
                    e.ToolTipSize = mySize;
                    myInternalImageWidth = mySize.Height;
                }
                else
                {
                    Size oldSize = e.ToolTipSize;
                    Control parent = e.AssociatedControl;
                    Image toolTipImage = parent.Tag as Image;
                    if (toolTipImage != null)
                    {
                        myInternalImageWidth = oldSize.Height;
                        oldSize.Width += myInternalImageWidth + PADDING;
                    }
                    else
                    {
                        oldSize.Width += PADDING;
                    }
                    e.ToolTipSize = oldSize;
                }
            }
        }
        void CustomizedToolTip_Draw(object sender, DrawToolTipEventArgs e)
        {
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            myToolTipRectangle.Size = e.Bounds.Size;
            e.Graphics.FillRectangle(myBorderBrush, myToolTipRectangle);
            myImageRectangle = Rectangle.Inflate(myToolTipRectangle,
                    -BORDER_THICKNESS, -BORDER_THICKNESS);
            e.Graphics.FillRectangle(myBackColorBrush, myImageRectangle);
            Control parent = e.AssociatedControl;
            Image toolTipImage = parent.Tag as Image;
            if (toolTipImage != null)
            {
                myImageRectangle.Width = myInternalImageWidth;
                myTextRectangle = new Rectangle(myImageRectangle.Right, myImageRectangle.Top,
                (myToolTipRectangle.Width - myImageRectangle.Right - BORDER_THICKNESS),
                            myImageRectangle.Height);
                myTextRectangle.Location =
            new Point(myImageRectangle.Right, myImageRectangle.Top);
                e.Graphics.FillRectangle(myBackColorBrush, myTextRectangle);
                e.Graphics.DrawImage(toolTipImage, myImageRectangle);
                e.Graphics.DrawString(e.ToolTipText, myFont,
                myTextBrush, myTextRectangle, myTextFormat);
            }
            else
            {
                e.Graphics.DrawString(e.ToolTipText, myFont,
                myTextBrush, myImageRectangle, myTextFormat);
            }
        }
    }
}
